/*
 * @Author: ✨Fireflx 
 * @Date: 2022-02-14 17:56:53 
 * @Last Modified by: ✨Fireflx
 * @Last Modified time: 2022-02-14 17:58:06
 */



const resCallback = require('../utils/response')

/**
 *
 *
 * @param {*} module
 * @param {*} ctx
 * @param {*} params
 */
const add = (module,ctx,params) => (
    //await 必须返回promise
    module.create(params).then(res => {
        resCallback({
            code:true,
            res,
            ctx,
            msg:'添加'
        })
    }).catch(err => {
        resCallback({
            res,
            ctx,
            msg:'添加'
        })
    })
)


/**
 *
 *
 * @param {*} module
 * @param {*} ctx
 * @param {*} where
 * @param {*} params
 */
const update = (module,ctx,where,params) => (
    module.updateOne(where, params).then(res => {
        resCallback({
            code:true,
            res,
            ctx,
            msg:'修改'
        })
    }).catch(err => {
        resCallback({
            res,
            ctx,
            msg:'修改'
        })
    })
)


/**
 *
 *
 * @param {*} module
 * @param {*} ctx
 * @param {*} where
 */
const del = (module,ctx,where) => (
    module.findOneAndDelete(where).then(res => {
        resCallback({
            code:true,
            res,
            ctx,
            msg:'删除'
        })
    }).catch(err => {
        resCallback({
            res,
            ctx,
            msg:'删除'
        })
    })
)


/**
 *
 *
 * @param {*} module
 * @param {*} ctx
 */
const find = (module,ctx) => (
    module.find().then(res => {
        resCallback({
            code:true,
            res,
            ctx,
            hasData:true,
            msg:'查询'
        })
    }).catch(err => {
        resCallback({
            res,
            ctx,
            msg:'查询'
        })
    })
)


/**
 *
 *
 * @param {*} module
 * @param {*} ctx
 * @param {*} where
 */
const findOne = (module,ctx,where) => (
    module.findOne(where).then(res => {
        resCallback({
            code:true,
            res,
            ctx,
            hasData:true,
            msg:'查询'
        })
    }).catch(err => {
        resCallback({
            res,
            ctx,
            msg:'查询'
        })
    })
)

module.exports = {
    add,
    del,
    update,
    find,
    findOne
}
